package cn.edu.ptu.jobfairs.core.security;

import cn.edu.ptu.jobfairs.basic.bo.Member;
import cn.edu.ptu.jobfairs.basic.common.User;
import cn.edu.ptu.jobfairs.core.SqlHandler;
import cn.edu.ptu.jobfairs.core.utils.SqlBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by Lin Chenxiao on 2020-05-26
 **/
@Slf4j
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private SqlHandler sqlHandler;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User user = sqlHandler.select(
                SqlBuilder.from("select * from # where # limit 1")
                        .append(Member.class).whereCrc("number", s), User.class);
        if (user == null) throw new UsernameNotFoundException("用户名不存在");
        return user;
    }
}
